﻿namespace Cmn.Cls
{
    using System;

    public class getPager
    {
        private int _doCount;
        private string _fldFields;
        private string _fldKey;
        private int _OrderType;
        private int _PageIndex;
        private int _PageSize;
        private int _State;
        private string _strOrder;
        private string _strWhere;
        private string _tblName;

        public getPager()
        {
            this._OrderType = -1;
            this._State = 0;
            this._tblName = "";
            this._fldKey = "";
            this._fldFields = "*";
            this._PageSize = 10;
            this._PageIndex = 1;
            this._strWhere = "";
            this._strOrder = "";
            this._doCount = 0;
            this._OrderType = -1;
        }

        public getPager(string tblName, string fldKey, string fldFields, int PageSize, int PageIndex, string strWhere, string strOrder, int orderType, int doCount)
        {
            this._OrderType = -1;
            this._State = 0;
            this._tblName = tblName;
            this._fldKey = fldKey;
            this._fldFields = fldFields;
            this._PageSize = PageSize;
            this._PageIndex = PageIndex;
            this._strWhere = strWhere;
            this._strOrder = strOrder;
            this._OrderType = orderType;
            this._doCount = doCount;
        }

        public string strSql()
        {
            if (this._OrderType == -1)
            {
                return this.strSqlUseNotIn();
            }
            return this.strSqlSingleOrder();
        }

        public string strSqlNoUseNotIn()
        {
            string str2 = "";
            string str3 = "";
            if (this._doCount != 0)
            {
                if (this._strWhere != "")
                {
                    return ("select count(" + this._fldKey + ") as Total from " + this._tblName + " where " + this._strWhere);
                }
                return ("select count(" + this._fldKey + ") as Total from " + this._tblName);
            }
            if (this._strOrder != "")
            {
                string[] strArray = this._strOrder.Split(new char[] { ',' });
                for (int i = 0; i <= (strArray.Length - 1); i++)
                {
                    string[] strArray2 = strArray[i].Trim().Split(new char[] { ' ' });
                    string[] strArray3 = strArray[i].Trim().Split(new char[] { '.' });
                    string str4 = (strArray2.Length == 1) ? (strArray2[0] + " desc") : (strArray2[0] + ((strArray2[strArray2.Length - 1] == "desc") ? " asc" : " desc"));
                    string[] strArray4 = str4.Trim().Split(new char[] { '.' });
                    str2 = (strArray4.Length == 1) ? (str2 + strArray4[0]) : (str2 + strArray4[1] + ",");
                    str3 = (strArray3.Length == 1) ? (str3 + strArray3[0]) : (str3 + strArray3[1] + ",");
                }
                str2 = " order by " + str2.TrimEnd(new char[] { ',' });
                str3 = " order by " + str3.TrimEnd(new char[] { ',' });
                this._strOrder = " order by " + this._strOrder;
            }
            if (this._PageIndex == 1)
            {
                if (this._strWhere != "")
                {
                    return ("select top " + this._PageSize.ToString() + " " + this._fldFields + " from " + this._tblName + " where " + this._strWhere + this._strOrder);
                }
                return ("select top " + this._PageSize.ToString() + " " + this._fldFields + " from " + this._tblName + " " + this._strOrder);
            }
            if (this._strWhere != "")
            {
                this._strWhere = " where " + this._strWhere;
            }
            return string.Concat(new object[] { 
                "select * from ( \tSELECT TOP ", this._PageSize.ToString(), " * from ( \t\tselect top ", this._PageSize * this._PageIndex, " ", this._fldFields, " \t\t\tfrom ", this._tblName, " \t\t\t", this._strWhere, " \t\t\t", this._strOrder, "  \t) as _a ", str2, " ) as _b ", str3, 
                " "
             });
        }

        public string strSqlSingleOrder()
        {
            string str3;
            string str = "";
            string str2 = "";
            if (this._doCount != 0)
            {
                if (this._strWhere != "")
                {
                    return ("select count(" + this._fldKey + ") as Total from " + this._tblName + " where " + this._strWhere);
                }
                return ("select count(" + this._fldKey + ") as Total from " + this._tblName);
            }
            string str4 = this._fldKey;
            if (this._fldKey.Split(new char[] { '.' }).Length > 1)
            {
                str4 = this._fldKey.Split(new char[] { '.' })[1];
            }
            if (this._OrderType != 0)
            {
                str2 = "<(select min";
                str3 = " order by " + this._strOrder + " desc";
            }
            else
            {
                str2 = ">(select max";
                str3 = " order by " + this._strOrder + " asc";
            }
            if (this._PageIndex == 1)
            {
                if (this._strWhere != "")
                {
                    return ("select top " + this._PageSize.ToString() + " " + this._fldFields + "  from " + this._tblName + " where " + this._strWhere + " " + str3);
                }
                return ("select top " + this._PageSize.ToString() + " " + this._fldFields + "  from " + this._tblName + " " + str3);
            }
            str = string.Concat(new object[] { 
                "select top ", this._PageSize.ToString(), " ", this._fldFields, "  from ", this._tblName, " where ", this._fldKey, str2, "(", str4, ") from (select top ", (this._PageIndex - 1) * this._PageSize, " ", this._fldKey, " from ", 
                this._tblName, " ", str3, ") as tblTmp) ", str3
             });
            if (this._strWhere != "")
            {
                str = string.Concat(new object[] { 
                    "select top ", this._PageSize.ToString(), " ", this._fldFields, "  from ", this._tblName, " where ", this._fldKey, str2, "(", str4, ") from (select top ", (this._PageIndex - 1) * this._PageSize, " ", this._fldKey, " from ", 
                    this._tblName, " where ", this._strWhere, " ", str3, ") as tblTmp) and ", this._strWhere, " ", str3
                 });
            }
            return str;
        }

        public string strSqlUseNotIn()
        {
            if (this._doCount != 0)
            {
                if (this._strWhere != "")
                {
                    return ("select count(" + this._fldKey + ") as Total from " + this._tblName + " where " + this._strWhere);
                }
                return ("select count(" + this._fldKey + ") as Total from " + this._tblName);
            }
            if (this._strOrder != "")
            {
                this._strOrder = " order by " + this._strOrder + " ";
            }
            if (this._PageIndex == 1)
            {
                if (this._strWhere != "")
                {
                    return ("select top " + this._PageSize.ToString() + " " + this._fldFields + " from " + this._tblName + " where " + this._strWhere + this._strOrder);
                }
                return ("select top " + this._PageSize.ToString() + " " + this._fldFields + " from " + this._tblName + " " + this._strOrder);
            }
            if (this._strWhere != "")
            {
                return string.Concat(new object[] { 
                    "select top ", this._PageSize, " ", this._fldFields, " from ", this._tblName, " where ( ", this._fldKey, " not in (select top ", this._PageSize * (this._PageIndex - 1), " ", this._fldKey, " from ", this._tblName, " where ", this._strWhere, 
                    this._strOrder, ")) and (", this._strWhere, ") ", this._strOrder
                 });
            }
            return string.Concat(new object[] { 
                "select top ", this._PageSize.ToString(), " ", this._fldFields, " from ", this._tblName, " where ( ", this._fldKey, " not in (select top ", this._PageSize * (this._PageIndex - 1), " ", this._fldKey, " from ", this._tblName, " ", this._strOrder, 
                ")) ", this._strOrder
             });
        }

        public int doCount
        {
            set
            {
                this._doCount = value;
            }
        }

        public string fldFields
        {
            set
            {
                this._fldFields = value;
            }
        }

        public string fldKey
        {
            set
            {
                this._fldKey = value;
            }
        }

        public int OrderType
        {
            set
            {
                this._OrderType = value;
            }
        }

        public int PageIndex
        {
            set
            {
                this._PageIndex = value;
            }
        }

        public int PageSize
        {
            set
            {
                this._PageSize = value;
            }
        }

        public int State
        {
            get
            {
                return this._State;
            }
        }

        public string strOrder
        {
            set
            {
                this._strOrder = value;
            }
        }

        public string strWhere
        {
            set
            {
                this._strWhere = value;
            }
        }

        public string tblName
        {
            set
            {
                this._tblName = value;
            }
        }
    }
}

